package com.young.shop.api.job;

import com.young.shop.api.dao.OrderMapper;
import com.young.shop.api.service.OrderService;
import com.young.shop.pojo.vo.OrderServiceVO;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import java.util.Date;
import java.util.List;

public class OrderTimeOutJob implements Job {
    @Autowired
    OrderMapper orderMapper;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
        //检查所有未支付订单列表(根据订单状态为1，查询所有的订单创建时间与订单id）
        List<OrderServiceVO> orderServiceVOS = orderMapper.selectByStatusOne();
        //判断订单是否超时(判断下单时间和当前时间是否超过15分钟，)
        //过期时间为15分钟
        int i;
        Long time = 15 * 60 * 1000L;  //十五分钟
        Date newDate = new Date(); //当前时间
        for (OrderServiceVO createData : orderServiceVOS) {
            Date expirationDate = new Date(createData.getCreateDate().getTime() + time);  //过期时间
            i = expirationDate.compareTo(newDate);
            //取消订单
            if (i<1){
                //根据订单id修改订单状态值
                orderMapper.upDateByOrderId(createData.getId());
            }
//            释放库存资源
        }
    }
}
